#include "fiter.h"
#include "string.h"

_iq FITER_movingAverage_update(struct movingAverage *self,_iq data)
{
    _iq sum;
    
    if (!self->isFirst)
    {
        sum = _IQ8mpy(self->ave, self->k0) + data;
        self->ave = _IQ8mpy(self->k, sum);
    }
    else 
    {
        self->ave = data;
        self->isFirst = 0;
    }

    return self->ave;
}

 /*********************************************************************
 * @fn      create_movingAverage
 *
 * @brief   滑动平均滤波器构造函数，
 *              window_size 窗口大小
 *
 * @return  smo_ *，成功  NULL，失败
 *          
 */
FITER_movingAverage_ *create_movingAverage(_iq window_size)
{
    FITER_movingAverage_ *pFiter = NULL;

    pFiter = (FITER_movingAverage_ *)malloc(sizeof(FITER_movingAverage_));
    if (!pFiter)
    {
        return NULL;
    }
    memset((void *)pFiter, 0, sizeof(FITER_movingAverage_));
    pFiter->window_size = _IQ8(window_size);
    pFiter->k = _IQ8((double)1 / window_size);
    pFiter->k0 = _IQ8(window_size - 1);
    pFiter->isFirst = 1;
    pFiter->update = FITER_movingAverage_update;
    
    return pFiter;
}